package com.alibaba.csp.sentinel.demo.zuul.gateway.sentinel;

import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * @author GaoZhiqiang
 * @date 2021-03-17 15:45:38
 */
public class MySentinelZuulPreFilter extends SentinelZuulPreFilter {

    public MySentinelZuulPreFilter() {
    }

    public MySentinelZuulPreFilter(int order) {
        super(order);
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("MySentinelZuulPreFilter");


        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();


      /*  String token = request.getHeader("token");

        if (StringUtils.isEmpty(token)) {
            request = new HttpServletRequestWrapper(request) {
                private final String token = "token";
                private Set<String> headerNameSet;

                @Override
                public Enumeration<String> getHeaderNames() {
                    if (headerNameSet == null) {
                        // first time this method is called, cache the wrapped request's header names:
                        headerNameSet = new HashSet<>();
                        Enumeration<String> wrappedHeaderNames = super.getHeaderNames();
                        while (wrappedHeaderNames.hasMoreElements()) {
                            String headerName = wrappedHeaderNames.nextElement();
                            if (!token.equalsIgnoreCase(headerName)) {
                                headerNameSet.add(headerName);
                            }
                        }
                    }
                    return Collections.enumeration(headerNameSet);
                }

                @Override
                public Enumeration<String> getHeaders(String name) {
                    if (token.equalsIgnoreCase(name)) {
                        return Collections.<String>emptyEnumeration();
                    }
                    return super.getHeaders(name);
                }

                @Override
                public String getHeader(String name) {
                    if (token.equalsIgnoreCase(name)) {
                        return null;
                    }
                    return super.getHeader(name);
                }
            };

        }

        token = request.getHeader("token");
        System.out.println("token 333 = " + token);

        ctx.setRequest(request);*/


        String noToken = request.getHeader("no-token");

        String ytoken = request.getHeader("y-token");

        System.out.println("notoken=" + noToken);


        System.out.println("ytoken=" + ytoken);

        return super.run();
    }
}
